package com.atguigu.process.controller;


import com.atguigu.auth.result.Result;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.process.service.OaProcessTemplateService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 审批模板 前端控制器
 * </p>
 *
 * @author 赵星
 * @since 2023-12-21
 */
@Api(value = "审批模板管理", tags = "审批模板管理")
@RestController
@RequestMapping(value = "/admin/process/processTemplate")
@Slf4j
public class OaProcessTemplateController {
    @Autowired
    private OaProcessTemplateService processTemplateService;

    //分页查询审批模板
    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit) {
        Page<ProcessTemplate> pageParam = new Page<>(page, limit);
        Page<ProcessTemplate> pageModel = processTemplateService.selectPage(pageParam);
        return Result.ok(pageModel);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.ok(processTemplate);
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public Result updateById(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    //@PreAuthorize("hasAuthority('bnt.processTemplate.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        processTemplateService.removeById(id);
        return Result.ok();
    }

//    @PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation("上传流程定义")
    @PostMapping("/uploadProcessDefinition")
    public Result uploadProcessDefinition(MultipartFile file) throws FileNotFoundException {
        //获取类路径的绝对路径
        String path = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();//中文路径存在问题
        path = "D:\\java17\\云上办公\\guigu-oa-parent\\service-oa\\target\\classes";
        //获取上传文件的文件名
        String filename = file.getOriginalFilename();
        //得到上传目录
        File tempFile = new File(path + "/processes/");
        System.out.println(tempFile);
        //判断目录是否存在，如果不存在则创建目录
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        //创建空文件用于接收上传的文件
        File imageFile = new File(tempFile + "/" + filename);
        log.info("上传至：" + imageFile);
        //保存文件到本地
        try {
            file.transferTo(imageFile);
        } catch (IOException e) {
            return Result.fail();
        }

        //根据上传地址后续部署流程定义，文件名称为流程定义的默认key
        Map<String, Object> map = new HashMap<>();
        map.put("processDefinitionPath", "processes/" + filename);
        map.put("processDefinitionKey", filename.substring(0, filename.lastIndexOf(".")));

        return Result.ok(map);
    }

//    @PreAuthorize("hasAuthority('bnt.processTemplate.publish')")
    @ApiOperation("发布审批模板")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id) {
        processTemplateService.publish(id);
        return Result.ok();
    }

}

